﻿using System;
using System.Collections.Generic;
using System.Windows;
using GameEditor.View.Components;
using GameEngine.Model.Data;
using GameEngine.View;
using GameEngine.Model;
using PureMVC.Interfaces;

namespace GameEditor.View
{
    /// <summary>
    /// </summary>
    public class GamePropertiesEditor_MenuMediator<IdType> : MenuMediator<IGameEditorMenuViewConstants.ButtonTags, IGameEditorMenuView<IdType>>
        where IdType : IComparable<IdType>
    {
        IGameDataProxy<IdType> dataProxy;

        public GamePropertiesEditor_MenuMediator(string name, IGameEditorMenuView<IdType> view, IGameDataProxy<IdType> dataProxy, string outCommand)
            : base(name, view, outCommand)
        {
            this.dataProxy = dataProxy;
        }

        public new IGameEditorMenuView<IdType> ViewComponent
        {
            get { return base.ViewComponent as IGameEditorMenuView<IdType>; }
        }

        #region IMediator Members

        /// <summary>
        /// List the <c>INotification</c> names this <c>Mediator</c> is interested in being notified of
        /// </summary>
        /// <returns>The list of <c>INotification</c> names </returns>
        override public IList<string> ListNotificationInterests()
        {
            IList<String> notifications = base.ListNotificationInterests();
            notifications.Add(dataProxy.LEVELADDED_NOTIFICATION);
            notifications.Add(dataProxy.LEVELREMOVED_NOTIFICATION);
            notifications.Add(dataProxy.THINGTYPEADDED_NOTIFICATION);
            notifications.Add(dataProxy.THINGTYPEREMOVED_NOTIFICATION);
            notifications.Add(dataProxy.VIEWADDED_NOTIFICATION);
            notifications.Add(dataProxy.VIEWREMOVED_NOTIFICATION);
            notifications.Add(dataProxy.VIEWSTATEADDED_NOTIFICATION);
            notifications.Add(dataProxy.VIEWSTATEREMOVED_NOTIFICATION);
            return notifications;
        }

        /// <summary>
        /// Handle <c>INotification</c>s
        /// </summary>
        /// <param name="notification">The <c>INotification</c> instance to handle</param>
        /// <remarks>
        ///     <para>
        ///        Typically this will be handled in a switch statement, with one 'case' entry per <c>INotification</c> the <c>Mediator</c> is interested in. 
        ///     </para>
        /// </remarks>
        override public void HandleNotification(INotification notification)
        {
            switch (notification.Name)
            {
                default:
                    
                    if (notification.Name.Equals(dataProxy.LEVELADDED_NOTIFICATION)) {
                        AddLevel(((ILevelDataProxy<IdType>)notification.Body).VO.Id);
                    }
                    else if (notification.Name.Equals(dataProxy.LEVELREMOVED_NOTIFICATION))
                    {
                        RemoveLevel((ILevelVOId)notification.Body);
                    }
                    else if (notification.Name.Equals(dataProxy.THINGTYPEADDED_NOTIFICATION))
                    {
                        AddThingType(((IThingTypeDataProxy<IdType>)notification.Body).VO.Id);
                    }
                    else if (notification.Name.Equals(dataProxy.THINGTYPEREMOVED_NOTIFICATION))
                    {
                        RemoveThingType((IThingTypeVOId)notification.Body);
                    }
                    else if (notification.Name.Equals(dataProxy.VIEWADDED_NOTIFICATION))
                    {
                        AddView(((IViewDataProxy<IdType>)notification.Body).VO.Id);
                    }
                    else if (notification.Name.Equals(dataProxy.VIEWREMOVED_NOTIFICATION))
                    {
                        RemoveView((IViewVOId<IdType>)notification.Body);
                    }
                    else if (notification.Name.Equals(dataProxy.VIEWSTATEADDED_NOTIFICATION))
                    {
                        AddViewState(((ViewStateVO)notification.Body));
                    }
                    else if (notification.Name.Equals(dataProxy.VIEWSTATEREMOVED_NOTIFICATION))
                    {
                        RemoveViewState((ViewStateVO)notification.Body);
                    }
                break;
            }
            base.HandleNotification(notification);
        }

        /// <summary>
        /// Called by the View when the Mediator is registered
        /// </summary>
        override public void OnRegister()
        {
            //ViewComponent.SetWorldMenu(worldMenuMediator.ViewComponent);
            //ViewComponent.SetMapMenu(tileMapMenuMediator.ViewComponent);
            //ViewComponent.SetMapView(worldMediator.ViewComponent);
            //ViewComponent.SetItemEditorView(itemEditorMediator.ViewComponent);

            //ViewComponent.MaxMapHeight = proxy.MaxHeight;
            //ViewComponent.MinMapHeight = proxy.MinHeight;
            //ViewComponent.TileSize = proxy.TileSize;
            //ViewComponent.TileHeightUnit = proxy.TileHeightUnit;

            //itemMenuMediator.

            //ViewComponent.OnSizeChanged += new Action<Point>(ViewComponent_OnSizeChanged);

            Name = dataProxy.Name;
            MaxMapHeight = dataProxy.MaxHeight;
            MinMapHeight = dataProxy.MinHeight;
            TileHeightUnit = dataProxy.TileHeightUnit;
            TileSize = dataProxy.TileSize;

            foreach (var obj in dataProxy.Levels)
            {
                AddLevel(obj.Key);
            }

            foreach (var obj in dataProxy.Resources)
                AddResource(obj.Key);

            foreach (var obj in dataProxy.ThingTypes)
                AddThingType(obj.Key);

            foreach (var obj in dataProxy.Views)
                AddView(obj.Key);

            foreach (var obj in dataProxy.ViewStates)
                AddViewState(obj);

        }

        /// <summary>
        /// Called by the View when the Mediator is removed
        /// </summary>
        override public void OnRemove()
        {
            //Facade.RemoveMediator(mapMediator.MediatorName);
            //Facade.RemoveMediator(Menu.MediatorName);
            // Facade.RemoveMediator(MenuBar.MediatorName);
        }

        #endregion




        public string Name
        {
            get { return ViewComponent.GameName; }
            set { ViewComponent.GameName = value; }
        }

        public float MaxMapHeight
        {
            get { return ViewComponent.MaxMapHeight; }
            set { ViewComponent.MaxMapHeight = value; }
        }

        public float MinMapHeight
        {
            get { return ViewComponent.MinMapHeight; }
            set { ViewComponent.MinMapHeight = value; }
        }

        public float TileHeightUnit
        {
            get { return ViewComponent.TileHeightUnit; }
            set { ViewComponent.TileHeightUnit = value; }
        }

        public Point TileSize
        {
            get { return ViewComponent.TileSize; }
            set { ViewComponent.TileSize = value; }
        }

        protected void AddResource(IResourceVOId id) { ViewComponent.AddResource(id); }
        protected void RemoveResource(IResourceVOId id) { ViewComponent.RemoveResource(id); }
        public List<IResourceVOId> GetResources() { return ViewComponent.GetResources(); }
        public IResourceVOId SelectedResource { get { return ViewComponent.SelectedResource; } }

        protected void AddLevel(ILevelVOId id) { ViewComponent.AddLevel(id); }
        protected void RemoveLevel(ILevelVOId id) { ViewComponent.RemoveLevel(id); }
        public List<ILevelVOId> GetLevels() { return ViewComponent.GetLevels(); }
        public ILevelVOId SelectedLevel { get { return ViewComponent.SelectedLevel; } }

        protected void AddView(IViewVOId<IdType> id) { ViewComponent.AddView(id); }
        protected void RemoveView(IViewVOId<IdType> id) { ViewComponent.RemoveView(id); }
        public List<IViewVOId<IdType>> GetViews() { return ViewComponent.GetViews(); }
        public IViewVOId<IdType> SelectedView { get { return ViewComponent.SelectedView; } }

        protected void AddViewState(IViewStateVO id) { ViewComponent.AddViewState(id); }
        protected void RemoveViewState(ViewStateVO id) { ViewComponent.RemoveViewState(id); }
        public List<IViewStateVO> GetViewStates() { return ViewComponent.GetViewStates(); }
        public IViewStateVO SelectedViewState { get { return ViewComponent.SelectedViewState; } }

        protected void AddThingType(IThingTypeVOId id) { ViewComponent.AddThingType(id); }
        protected void RemoveThingType(IThingTypeVOId id) { ViewComponent.RemoveThingType(id); }
        public List<IThingTypeVOId> GetThingTypes() { return ViewComponent.GetThingTypes(); }
        public IThingTypeVOId SelectedThingType { get { return ViewComponent.SelectedThingType; } }
    }
}